package com.jiayang.business.homepage;

import com.jiayang.base.constant.KeyConstant;
import com.jiayang.base.equipment.StaEquipmentMapper;
import com.jiayang.base.equipment.domain.dto.EquipmentFindDTO;
import com.jiayang.base.equipment.domain.entity.StaEquipment;
import com.jiayang.base.sensor.StaSensorMapper;
import com.jiayang.base.sensor.domain.entity.StaSensor;
import com.jiayang.base.station.StaStationMapper;
import com.jiayang.base.station.domain.dto.StationFindDTO;
import com.jiayang.base.system.employee.domain.bo.EmployeeBO;
import com.jiayang.base.system.login.domain.RequestTokenBO;
import com.jiayang.business.homepage.constant.HomePageResponseCodeConst;
import com.jiayang.business.homepage.domain.dto.*;
import com.jiayang.common.domain.ResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     ：OuyangChenglong
 * @ Date       ：Created in 10:11 2021/9/8
 * @ Description：总部首页服务层
 * @ Modified By：
 * @Version: $
 */
@Service
public class HomePageService {

    @Autowired
    private StaStationMapper stationDao;

    @Autowired
    private StaEquipmentMapper staEquipmentMapper;

    @Autowired
    private StaSensorMapper sensorDao;

    @Autowired
    private HomePageDao homePageDao;

    /**
     * 获取该用户名下所有场站和设备名称
     * @param requestToken
     * @return
     */
    public ResponseDTO<List<ResponseStationDTO>> getStationAndEquipment(RequestTokenBO requestToken) {
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        List<ResponseStationDTO> dtoList = new ArrayList<>();
        // 根据当前登录用户id查找对应场站
        List<StationFindDTO> station = stationDao.getStationByEmpId(uid);
        if (station == null){
            return ResponseDTO.wrap(HomePageResponseCodeConst.STATION_NOT_EXISTS);
        }
        for (int i = 0; i < station.size(); i++){
            // 根据场站查找对应设备
            StationFindDTO stationFindDTO = station.get(i);
            String sid = stationFindDTO.getId();
            List<EquipmentFindDTO> equipmentList = staEquipmentMapper.getEquipmentByStationId(sid);
            ResponseStationDTO responseStationDTO = new ResponseStationDTO(equipmentList,stationFindDTO);
            dtoList.add(i,responseStationDTO);
        }
        return ResponseDTO.succData(dtoList);
    }

    /**
     * 获取所有符合当前登录用户的所有设备运行情况
     * @param requestToken
     * @return
     */
    public ResponseDTO getAllEquipmentState(RequestTokenBO requestToken) {
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        List<StaEquipment> list = staEquipmentMapper.getAllEquipmentByUserId(uid);
        // 停机设备数
        int downVal = 0;
        // 正常运行设备数
        int normalVal = 0;
        // 预警设备数
        int warningVal = 0;
        // 告警设备数
        int errorVal = 0;
        for (int i = 0; i < list.size(); i++){
            StaEquipment equipmentEntity = list.get(i);
            String state = equipmentEntity.getState();
            int parseInt = Integer.parseInt(state);
            if (parseInt == 0){
                downVal++;
            }else if (parseInt == 1){
                normalVal++;
            }else if (parseInt == 2){
                warningVal++;
            }else {
                errorVal++;
            }
        }
        HashMap<String, Integer> data = new HashMap<>();
        data.put(KeyConstant.NUMBER_OF_SHUT_DOWN,downVal);
        data.put(KeyConstant.NUMBER_OF_NORMAL,normalVal);
        data.put(KeyConstant.NUMBER_OF_WARN,warningVal);
        data.put(KeyConstant.NUMBER_OF_ERROR,errorVal);
        return ResponseDTO.succData(data);
    }

    /**
     * 获取所有符合当前登录用户的所有设备故障类型
     * @param requestToken
     * @return
     */
    public ResponseDTO getErrorType(RequestTokenBO requestToken) {
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        // 根据用户id查询对应所有设备
        List<StaEquipment> list = staEquipmentMapper.getAllEquipmentByUserId(uid);
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++){
            StaEquipment equipmentEntity = list.get(i);
            String errorType = equipmentEntity.getErrorType();
            map.put("errorType",errorType);
        }
        return ResponseDTO.succData(map);
    }

    /**
     * 获取传感器运行情况
     * @param requestToken
     * @return
     */
    public ResponseDTO getAllSensorState(RequestTokenBO requestToken) {
        // 正常传感器数
        int normal = 0;
        // 报警传感器数
        int error = 0;
        // 需要校准传感器数
        int needCheck = 0;
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        // 根据用户id查询对应所有传感器
        List<StaSensor> list = sensorDao.getAllSensorByUserId(uid);
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++){
            StaSensor StaSensor = list.get(i);
            String state = StaSensor.getState();
            int parseInt = Integer.parseInt(state);
            if (parseInt == 0){
                error++;
            }else if (parseInt == 1){
                normal++;
            }else {
                needCheck++;
            }
        }
        map.put(KeyConstant.NUMBER_OF_SENSOR_ERROR,error);
        map.put(KeyConstant.NUMBER_OF_NORMAL,normal);
        map.put(KeyConstant.NUMBER_OF_SENSOR_NEED_CHENCK,needCheck);
        return ResponseDTO.succData(map);
    }

    /**
     * 获取统计项每日关机数
     * @param requestToken
     * @return
     */
    public ResponseDTO getDailyDown(RequestTokenBO requestToken) {
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        // 根据用户id查找符合统计项
        List<DailyDownDTO> list = homePageDao.getDailyDown(uid);
        return ResponseDTO.succData(list);
    }

    public ResponseDTO getEnergyConsumption(RequestTokenBO requestToken) {
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        // 根据用户id查找符合统计项
        List<EnergyConsumptionDTO> list = homePageDao.getEnergyConsumption(uid);
        return ResponseDTO.succData(list);
    }

    public ResponseDTO getDailyAlarm(RequestTokenBO requestToken) {
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        // 根据用户id查找符合统计项
        List<DailyAlarmDTO> list = homePageDao.getDailyAlarm(uid);
        return ResponseDTO.succData(list);
    }

    public ResponseDTO getOnlineEquipment(RequestTokenBO requestToken) {
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        // 根据用户id查找符合统计项
        List<OnlineEquipmentDTO> list = homePageDao.getOnlineEquipment(uid);
        return ResponseDTO.succData(list);
    }

    public ResponseDTO getOnlineSensor(RequestTokenBO requestToken) {
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        // 根据用户id查找符合统计项
        List<OnlineSensorDTO> list = homePageDao.getOnlineSensor(uid);
        return ResponseDTO.succData(list);
    }

    public ResponseDTO getTopFive(RequestTokenBO requestToken) {
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        // 根据用户id查找符合统计项
        List<TopFiveDTO> list = homePageDao.getTopFive(uid);
        return ResponseDTO.succData(list);
    }

    public ResponseDTO getInfo(RequestTokenBO requestToken) {
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        // 根据用户id查找软件信息
        InfoDTO info = homePageDao.getInfo(uid);
        return ResponseDTO.succData(info);
    }

    public ResponseDTO updateFeedBack(FeedBackDTO feedbackDto, RequestTokenBO requestToken) {
        EmployeeBO employeeBO = requestToken.getEmployeeBO();
        Long uid = employeeBO.getId();
        // 根据用户id查找符合更新的用户反馈
        String feedback = feedbackDto.getFeedback();
        homePageDao.updateFeedBack(uid,feedback);
        return ResponseDTO.succ();
    }
}
